import 'package:demo1223/eventbus/eventhbus.dart';
import 'package:demo1223/model/category.dart';
import 'package:demo1223/service/service_method.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../toast/toast.dart';

int goodid;
String token = "";

class DerailsCart extends StatelessWidget {
  @override
  String url;
  String name;
  String price1;
  String price2;
  Widget build(BuildContext context) {
    aaa();
    gettoken();
    return Container(
      width: ScreenUtil().setWidth(750),
      height: ScreenUtil().setHeight(100),
      color: Colors.white,
      child: Row(
        children: <Widget>[
          InkWell(
            onTap: () {},
            child: Container(
              width: ScreenUtil().setWidth(120),
              height: ScreenUtil().setHeight(100),
              alignment: Alignment.center,
              child: Icon(
                Icons.shopping_cart,
                size: 35,
                color: Colors.red,
              ),
            ),
          ),
          InkWell(
            onTap: () {
              showModalBottomSheet(
                  context: context,
                  builder: (BuildContext context) {
                    return _cartmenu(context);
                  });
            },
            child: Container(
              width: ScreenUtil().setWidth(315),
              alignment: Alignment.center,
              height: ScreenUtil().setHeight(100),
              color: Colors.green,
              child: Text(
                '加入购物车',
                style: TextStyle(
                    color: Colors.black, fontSize: ScreenUtil().setSp(28)),
              ),
            ),
          ),
          InkWell(
            onTap: () {},
            child: Container(
              width: ScreenUtil().setWidth(315),
              alignment: Alignment.center,
              height: ScreenUtil().setHeight(100),
              color: Colors.red,
              child: Text(
                '立即购买',
                style: TextStyle(
                    color: Colors.black, fontSize: ScreenUtil().setSp(28)),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _cartmenu(BuildContext context) {
    return Container(
      height: 350.0,
      color: Colors.white,
      child: Column(
        children: <Widget>[
          _show(),
          Addition(),
        ],
      ),
    );
  }

  Widget _show() {
    return Row(
      children: <Widget>[_imgsa(), _content()],
    );
  }

  Widget _imgsa() {
    return Image.network(
      url,
      width: ScreenUtil().setWidth(180),
    );
  }

  Widget _content() {
    return Container(
      width: ScreenUtil().setWidth(550),
      child: Column(
        children: <Widget>[
          _name(),
          pricea(),
          priceb(),
        ],
      ),
    );
  }

  Widget _name() {
    return Container(
        child: Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Expanded(
              child: Text(name),
            )
          ],
        )
      ],
    ));
  }

  Widget pricea() {
    return Row(
      children: <Widget>[
        Text("会员价：${price1}"),
      ],
    );
  }

  Widget priceb() {
    return Row(
      children: <Widget>[
        Text(
          "市场价：${price2}",
          style: TextStyle(
              decoration: TextDecoration.lineThrough, color: Colors.black45),
        )
      ],
    );
  }

  void aaa() {
    eventBus.on<Details>().listen((event) => {
          url = event.url,
          name = event.name,
          price1 = event.price1,
          price2 = event.price2,
          goodid = event.goodid,
        });
  }

  void gettoken() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    token = prefs.getString('userInfo');
    print(token);
  }
}

class Addition extends StatefulWidget {
  @override
  _AdditionState createState() => _AdditionState();
}

class _AdditionState extends State<Addition> {
  var nums = 0;
  @override
  Widget build(BuildContext context) {
    print(nums);
    return Container(
      padding: EdgeInsets.only(left: 20, right: 20),
      margin: EdgeInsets.only(top: 50),
      child: Column(
        children: <Widget>[numconten(), addcart()],
      ),
    );
  }

  Widget numconten() {
    return Container(
      child: Row(
        children: <Widget>[
          Text(
            "购买数量",
            style: TextStyle(fontSize: ScreenUtil().setSp(30)),
          ),
          GestureDetector(
            child: Container(
              width: 40.0,
              height: 30.0,
              decoration: BoxDecoration(
                  border: Border.all(width: 1, color: Colors.black45)),
              child: Icon(Icons.remove),
              margin: EdgeInsets.only(left: 150),
            ),
            onTap: () {
              setState(() {
                if (nums <= 0) {
                  Toast.show("数量已经最少啦");
                  return;
                }
                nums--;
              });
            },
          ),
          numss(),
          GestureDetector(
            child: Container(
              decoration: BoxDecoration(
                  border: Border.all(width: 1, color: Colors.black45)),
              width: 40.0,
              height: 30.0,
              child: Icon(
                Icons.add,
              ),
            ),
            onTap: () {
              setState(() {
                if (nums >= 999999999) {
                  return;
                }
                nums++;
              });
            },
          ),
        ],
      ),
    );
  }

  Widget numss() {
    return Container(
      height: 30,
      width: 60,
      alignment: Alignment.center,
      decoration: BoxDecoration(
          border: Border(
              top: BorderSide(width: 1, color: Colors.black45),
              bottom: BorderSide(width: 1, color: Colors.black45))),
      child: Center(
        child: Text(
          "${nums}",
        ),
      ),
    );
  }

  Widget addcart() {
    return Container(
      margin: EdgeInsets.only(top: 50),
      child: Row(
        children: <Widget>[
          RaisedButton(
            child: Text('加入购物车'),
            padding: EdgeInsets.only(left: 150, right: 150, top: 5, bottom: 5),
            color: Colors.red,
            onPressed: () {
              print('加入购物车');
              var formdata = {
                'token': token,
                'goods_id': goodid,
                'cart_num': nums,
                'is_checked': '',
                'spec_values': ''
              };
              DioUtil.request('addOrderCart', formData: formdata)
                  .then((val) => {print(val)});
              Toast.show('加入购物车成功');
            },
          )
        ],
      ),
    );
  }
}
